package headertea;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.Query;

import com.sun.xml.internal.messaging.saaj.packaging.mime.Header;

import util.HibernateUtil;
import headertea.HeaderTeacherDao;
import entity.HeaderTeacher;
import entity.classCourse;

public class HeaderTeacherDaoImpl implements HeaderTeacherDao{

	//鐝富浠绘潈闄愬鎵�
	@Override
	public boolean checkTeacher(HeaderTeacher headerteacher) {
		Session session = null;
		Transaction tran = null;
		try{
			session = HibernateUtil.openSession();
			tran = session.beginTransaction();
			session.save(headerteacher);
			tran.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
			return false;
		} finally {
			if (null != session) {
				session.close();
			}
		}
	}
	
	//鏌ョ湅鎵�鏈夋嫢鏈夌彮涓讳换鏉冮檺鐨勬暀甯�
	public List<HeaderTeacher> findAll(){
		Session session = HibernateUtil.openSession();
	    Transaction transaction = null;
	    try {
	    	transaction = session.beginTransaction();

	        
	        List<HeaderTeacher> list = session.createQuery("FROM HeaderTeacher").list(); 

	        transaction.commit();
	        return list;
	    } catch (RuntimeException e) {
	   	transaction.rollback();
	        throw e;
	    } finally {
	        session.close();
	    }
	}
	
	//鐝富浠绘潈闄愭坊鍔�
	@Override
	public boolean addHeaderTeacher(HeaderTeacher headerteacher) {
		String mess = "error";
		Session session = HibernateUtil.openSession();
		try {
			Transaction transaction = session.beginTransaction();
			session.save(headerteacher);
			transaction.commit();
			return true;
		} catch (Exception e) {
			//message("RegisterInfo error:" + e);
			e.printStackTrace();
			return false;
		} finally {
			if (null != session) {
				session.close();
			}
		}
	}
	
	//鏍规嵁鐝富浠籭d鍒犻櫎鐝富浠绘潈闄�
	@Override
	public boolean delHeaderTeacher(int id) {
		Session session = HibernateUtil.openSession();
		try {
			Transaction transaction = session.beginTransaction();
			Query query = session.createQuery("from HeaderTeacher where id=?");
			query.setParameter(0, id);
			HeaderTeacher headerteacher = (HeaderTeacher)query.uniqueResult();
			session.delete(headerteacher);
			transaction.commit();
			return true;
		} catch (Exception e){
			e.printStackTrace();
			return false;
		} finally {
			if (null != session) {
				session.close();
			}
		}
}
}